package com.hyt.it.ogt.download.service.impl;

import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.hyt.core.util.UUIDUtils;
import com.hyt.it.ogt.download.config.ConfigManager;
import com.hyt.it.ogt.download.config.MonitorBizRunTimeException;
import com.hyt.it.ogt.download.enums.BizStatusCode;
import com.hyt.it.ogt.download.mapper.VideoMapper;
import com.hyt.it.ogt.download.service.IUserService;
import com.hyt.loginfo.model.UserPermissionQueryVo;
import com.hyt.loginfo.service.IUaUserExtService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class UserServiceImpl implements IUserService {
	
    public static final String MONITOR_USER_TOKEN = "MONITOR_USER_TOKEN";
    
    @Resource
	private RedisTemplate<String, String> redisTemplate;
    @Resource
    private ConfigManager configManager;
    @Resource
    private IUaUserExtService iUaUserExtService;
    @Resource
    private VideoMapper videoMapper;

    /**
     * 20000：成功, 12175101:获取不到用户信息, 12175102:用户名密码不正确,12175103:权限标识并不存在, 12175104:用户暂无权限
     *
     * @param username
     * @param password
     */
    @Override
    public String login(String username, String password, String examCode) {
        // 判断是否有预制账户
        int count = videoMapper.validLocalUserExist(username, password);
        String officeId = null;
        if(count == 0) {
            UserPermissionQueryVo p = iUaUserExtService.queryUserOfficeIdByPermission(username, password, configManager.getVideoPermission());
            log.info(JSON.toJSONString(p));
            if (p.getCode().intValue() == BizStatusCode.SUCCESS_CODE.getCode().intValue()) {
                officeId = videoMapper.getOfficeIdByExamId(examCode);
                if (StringUtils.isBlank(officeId)) {
                    throw new MonitorBizRunTimeException(BizStatusCode.SEARCH_NONE.getCode(), BizStatusCode.SEARCH_NONE.getMsg());
                } else if (!officeId.equalsIgnoreCase(p.getOfficeid())) {
                    throw new MonitorBizRunTimeException(BizStatusCode.NOT_PERMISSION.getCode(), BizStatusCode.NOT_PERMISSION.getMsg());
                }
            } else if (p.getCode().intValue() == BizStatusCode.USER_LOGIN_NO_PERMISSION.getCode().intValue()) {
                throw new MonitorBizRunTimeException(BizStatusCode.LOGIN_NO_PERMISSION.getCode(), BizStatusCode.LOGIN_NO_PERMISSION.getMsg());
            } else {
                throw new MonitorBizRunTimeException(BizStatusCode.LOGIN_WRONG_PASSWORD.getCode(), BizStatusCode.LOGIN_WRONG_PASSWORD.getMsg());
            }
        } else {
            officeId = videoMapper.getOfficeIdByExamId(examCode);
        }
        // 
        if (StringUtils.isBlank(officeId)) {
        	throw new MonitorBizRunTimeException(BizStatusCode.SEARCH_NONE.getCode(), BizStatusCode.SEARCH_NONE.getMsg());
        }
        String token = UUIDUtils.randomUUID();
        String key = StringUtils.joinWith(":", MONITOR_USER_TOKEN, token);
        redisTemplate.opsForValue().set(key, officeId, 7, TimeUnit.DAYS);
        return token;
    }
}
